package com.wxxymaker.service;

import com.wxxymaker.model.dao.DeletedStageCountDao;
import com.wxxymaker.model.entity.DeletedStageCount;
import com.wxxymaker.model.entity.StageCount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional
public class DeletedStageCountService {

    @Autowired
    private DeletedStageCountDao stageCountDao;

    /**
     * 获取所有阶段的名和id
     *
     * @return 阶段列表
     */
    public List<Map<String, Object>> findStageNames() {
        List<Object[]> stageNames = stageCountDao.findStageNames();
        return stageNames.stream().map(val -> {
            Map<String, Object> map = new HashMap<>();
            map.put("stageId", val[0]);
            map.put("stageName", val[1]);
            return map;
        }).collect(Collectors.toList());
    }

    /**
     * 分页查询阶段数据
     *
     * @param pageNo 页码
     * @param size   尺寸
     * @param map    查询条件
     * @return 分页数据
     */
    public Page<DeletedStageCount> page(Integer pageNo, Integer size, Map<String, Object> map) {
        Specification<DeletedStageCount> specification = new Specification<DeletedStageCount>() {
            @Override
            public Predicate toPredicate(Root<DeletedStageCount> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                if (map == null)
                    return null;
                List<Predicate> ps = new LinkedList<>();
                map.forEach((k, v) -> {
                    if (!"".equals(v)) {
                        switch (k) {
                            case "shop_id":
                                ps.add(cb.equal(root.get("shop").get("id"), v));
                                break;
                            case "stage_id":
                                ps.add(cb.equal(root.get("stageId"), v));
                                break;
                            default:
                                ps.add(cb.equal(root.get(k), v));
                                break;
                        }
                    }
                });
                query.where(ps.toArray(new Predicate[0]));
                return null;
            }
        };

        Pageable pageable = new PageRequest(pageNo - 1, size, new Sort(Sort.Direction.DESC, "countTime"));
        return stageCountDao.findAll(specification, pageable);
    }
}
